package com.zoe.onelink.common.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 接口基础响应对象
 * @param <T>
 */
@Data
@Accessors(chain = true)
public class ApiResult<T> implements Serializable, IResult<T> {
    private static final long serialVersionUID = 1L;
    public static final String CODE_SUCCESS = "0";
    public static final String MESSAGE_SUCCESS = "success";
    /**消息提示*/
    private String message;
    /**业务状态码 http状态为200 情况下 0为成功*/
    private String code;
    /**业务数据*/
    private T data;
    /**错误信息*/
    private String error;

    /**
     * 判断是否业务操作成功
     * @return
     */
    public boolean checkSuccess() {
        // 兼容5个0,否则所有服务无法联调
        return CODE_SUCCESS.equals(this.code) || "00000".equals(this.code);
    }

    public ApiResult(T data) {
        this();
        this.data = data;

    }

    public ApiResult() {
        this.code = CODE_SUCCESS;
        this.message = MESSAGE_SUCCESS;
    }


    public static <T> ApiResult<T> success() {
        return success(null);
    }

    public static <T> ApiResult<T> success(T data) {
        return success(MESSAGE_SUCCESS, data);
    }

    public static <T> ApiResult<T> success(String message, T data) {
        return new ApiResult<T>()
                .setMessage(message)
                .setCode(CODE_SUCCESS)
                .setData(data);
    }

}